<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Calling it from C - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../experimental/c-api/example.html"><img src="../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../experimental/c-api.html"><img src="../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/c-api/reference.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">Calling it from C</h1></div></div></div>
  <p>Firstly we need to declare to C our <code>result</code> returning C++ function:</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="c1">// Declare our C++ function&#39;s returning result type. Only needs to be done once.
</span><span class="c1">// This declares an `status_result&lt;size_t, system_code&gt;` which is an alias to
</span><span class="c1">// `basic_result&lt;size_t, status_code&lt;erased&lt;intptr_t&gt;&gt;&gt;`.
</span><span class="c1">//
</span><span class="c1">// The first parameter is some unique identifier for this type which will be used
</span><span class="c1">// whenever we reference this type in the future.
</span><span class="c1"></span><span class="n">CXX_DECLARE_RESULT_SYSTEM</span><span class="p">(</span><span class="n">to_string_rettype</span><span class="p">,</span> <span class="n">size_t</span><span class="p">);</span>

<span class="c1">// Tell C about our extern C++ function `to_string()`
</span><span class="c1"></span><span class="k">extern</span> <span class="nf">CXX_RESULT_SYSTEM</span><span class="p">(</span><span class="n">to_string_rettype</span><span class="p">)</span> <span class="n">_Z9to_stringPcmi</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span><span class="n">buffer</span><span class="p">,</span> <span class="n">size_t</span> <span class="n">bufferlen</span><span class="p">,</span> <span class="kt">int</span> <span class="n">v</span><span class="p">);</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/c_api.c#L40" class="code-snippet-url" target="_blank">View this code on Github</a></div>


<p>Now let&rsquo;s call the C++ function from C:</p>

<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="kt">void</span> <span class="nf">print</span><span class="p">(</span><span class="kt">int</span> <span class="n">v</span><span class="p">)</span>
<span class="p">{</span>
  <span class="kt">char</span> <span class="n">buffer</span><span class="p">[</span><span class="mi">4</span><span class="p">];</span>
  <span class="n">CXX_RESULT_SYSTEM</span><span class="p">(</span><span class="n">to_string_rettype</span><span class="p">)</span> <span class="n">res</span><span class="p">;</span>

  <span class="n">res</span> <span class="o">=</span> <span class="n">_Z9to_stringPcmi</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">buffer</span><span class="p">),</span> <span class="n">v</span><span class="p">);</span>
  <span class="k">if</span><span class="p">(</span><span class="n">CXX_RESULT_HAS_VALUE</span><span class="p">(</span><span class="n">res</span><span class="p">))</span>
  <span class="p">{</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&#34;to_string(%d) fills buffer with &#39;%s&#39; of %zu characters</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="n">buffer</span><span class="p">,</span> <span class="n">res</span><span class="p">.</span><span class="n">value</span><span class="p">);</span>
    <span class="k">return</span><span class="p">;</span>
  <span class="p">}</span>
  <span class="c1">// Is the error returned in the POSIX domain and thus an errno?
</span><span class="c1"></span>  <span class="k">if</span><span class="p">(</span><span class="n">CXX_RESULT_ERROR_IS_ERRNO</span><span class="p">(</span><span class="n">res</span><span class="p">))</span>
  <span class="p">{</span>
    <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&#34;to_string(%d) failed with error code %d (%s)</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="p">(</span><span class="kt">int</span><span class="p">)</span> <span class="n">res</span><span class="p">.</span><span class="n">error</span><span class="p">.</span><span class="n">value</span><span class="p">,</span> <span class="n">strerror</span><span class="p">((</span><span class="kt">int</span><span class="p">)</span> <span class="n">res</span><span class="p">.</span><span class="n">error</span><span class="p">.</span><span class="n">value</span><span class="p">));</span>
    <span class="k">return</span><span class="p">;</span>
  <span class="p">}</span>
  <span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span> <span class="s">&#34;to_string(%d) failed with unknown error code %lld</span><span class="se">\n</span><span class="s">&#34;</span><span class="p">,</span> <span class="n">v</span><span class="p">,</span> <span class="p">(</span><span class="kt">long</span> <span class="kt">long</span><span class="p">)</span> <span class="n">res</span><span class="p">.</span><span class="n">error</span><span class="p">.</span><span class="n">value</span><span class="p">);</span>
<span class="p">}</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
  <span class="n">print</span><span class="p">(</span><span class="mi">9</span><span class="p">);</span>
  <span class="n">print</span><span class="p">(</span><span class="mi">99</span><span class="p">);</span>
  <span class="n">print</span><span class="p">(</span><span class="mi">999</span><span class="p">);</span>
  <span class="n">print</span><span class="p">(</span><span class="mi">9999</span><span class="p">);</span>
  <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/c_api.c#L53" class="code-snippet-url" target="_blank">View this code on Github</a></div>


<p>Running this C program yields:</p>

<pre><code>to_string(9) fills buffer with '9' of 1 characters
to_string(99) fills buffer with '99' of 2 characters
to_string(999) fills buffer with '999' of 3 characters
to_string(9999) failed with error code 105 (No buffer space available)
</code></pre>


        </div><p><small>Last revised: February 05, 2019 at 17:14:18 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../experimental/c-api/example.html"><img src="../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../experimental/c-api.html"><img src="../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/c-api/reference.html"><img src="../../images/next.png" alt="Next"></a></div></body>
</html>
